package com.zhizhi.domain;


import com.zhizhi.common.constant.ResponseCodeConst;
import lombok.Data;

/*
 * description:  通用API接口返回DTO<br>
 * date: 2020/7/23 9:21 <br>
 * author: Mr.Yu
 */
@Data
public class ResponseDTO<T> {

    /**
     * 返回状态码
     */
    private Integer status;

    /**
     * 返回消息
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    public ResponseDTO() {

    }

    public ResponseDTO(Integer code, String message) {
        this.status = code;
        this.message = message;
    }

    public ResponseDTO(ResponseCodeConst responseCodeConst) {
        this.message = responseCodeConst.getMsg();
        this.status = responseCodeConst.getCode();
    }

    public ResponseDTO(ResponseCodeConst responseCodeConst, T data) {
        this.message = responseCodeConst.getMsg();
        this.status = responseCodeConst.getCode();
        this.data = data;
    }

    public ResponseDTO(ResponseCodeConst responseCodeConst, String msg) {
        this.status = responseCodeConst.getCode();
        this.message = msg;
    }

    public static <T> ResponseDTO<T> wrap(ResponseCodeConst codeConst) {
        return new ResponseDTO<>(codeConst);
    }

    public static <T>  ResponseDTO<T> wrap(ResponseCodeConst codeConst, T t) {
        return new  ResponseDTO<T>(codeConst, t);
    }

    public static <T>  ResponseDTO<T> wrap(ResponseCodeConst codeConst, String msg) {
        return new ResponseDTO<T>(codeConst, msg);
    }

    /**
     * 操作成功
     * @param <T> 返回数据类型
     * @return 成功JSON数据
     */
    public static <T> ResponseDTO<T> success() {
        return new ResponseDTO<T>(ResponseCodeConst.SUCCESS);
    }

    public static <T> ResponseDTO<T> successWithMessage(Integer code, String msg) {
        return new ResponseDTO<T>(code,msg);
    }



    /**
     * 带数据的操作成功
     * @param data 返回数据
     * @param <T> 返回数据类型
     * @return 返回操作成功的JSON数据
     */
    public static <T> ResponseDTO<T> successWithData(T data) {
        return new ResponseDTO<T>(ResponseCodeConst.SUCCESS, data);
    }

    /**
     * GET/POST方法使用不当返回结果
     * @param <T> 返回数据类型
     * @return 请求方法不支持结果JSON数据
     */
    public static <T> ResponseDTO<T> methodNotSupport() {
        return new ResponseDTO<T>(ResponseCodeConst.METHOD_NOT_SUPPORT);
    }

    /**
     * 请求参数错误
     * @param <T> 返回数据类型
     * @return 请求参数错误返回结果
     */
    public static <T> ResponseDTO<T> badRequest() {
        return new ResponseDTO<T>(ResponseCodeConst.BAD_REQUEST);
    }

    /**
     * 禁止访问
     * @param <T> 返回数据类型
     * @return 禁止访问结果
     */
    public static <T> ResponseDTO<T> forbidden() {
        return new ResponseDTO<T>(ResponseCodeConst.FORBIDDEN);
    }

    /**
     * 请求未携带token或token已过期
     * @param <T> 返回数据类型
     * @return 未登录请求返回结果
     */
    public static <T> ResponseDTO<T> notLogin() {
        return new ResponseDTO<T>(ResponseCodeConst.UNAUTHORIZED);
    }

    /**
     * 服务器内部错误
     * @param <T> 返回数据类型
     * @return 服务器内部错误返回结果
     */
    public static <T> ResponseDTO<T> serverError() {
        return new ResponseDTO<T>(ResponseCodeConst.INTERNAL_SERVER_ERROR);
    }

    /**
     * 请求资源不存在
     * @param <T>返回数据类型
     * @return 请求资源不存在返回结果
     */
    public static <T> ResponseDTO<T> notFound() {
        return new ResponseDTO<T>(ResponseCodeConst.NOT_FOUND);
    }

    /**
     * 参数校验失败
     */
    public static <T> ResponseDTO<T> paramsError(Integer code, String msg){
        return new ResponseDTO<T>(code,msg);
    }

}